package demo.hello;

import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

public class DeleteFiles {

	private Frame main;
	private Button b1, b2, b3, db;
	private TextArea ta;
	private TextField tf1, tf2;
	private File f;
	private ArrayList<File> files;
	private Dialog d;
	private Label lab, lab1, lab2;

	private void init() {
		main = new Frame("����ɾ���ļ�");
		main.setBounds(700, 300, 400, 550);
		main.setLayout(new FlowLayout());
		b1 = new Button("��ͷ");
		b2 = new Button("����");
		b3 = new Button("��׺");
		ta = new TextArea(27, 50);
		tf1 = new TextField(40);
		tf2 = new TextField(10);
		lab1 = new Label("�����ַ");
		lab2 = new Label("�����ֶ�");
		main.add(lab1);
		main.add(tf1);
		main.add(lab2);
		main.add(tf2);
		main.add(b1);
		main.add(b2);
		main.add(b3);
		main.add(ta);
		addEvent();
		main.setVisible(true);
	}

	private DeleteFiles() {
		init();
	}

	private void dialog1() {
		d = new Dialog(main, "��ʾ", true);
		d.setLayout(new FlowLayout());
		db = new Button("ȷ��");
		lab = new Label("�����ڴ˵�ַ������������");
		d.setBounds(800, 350, 200, 120);
		addWin();
		addAct();
		d.add(lab);
		d.add(db);
		d.setVisible(true);
	}

	private void dialog2() {
		d = new Dialog(main, "��ʾ", true);
		d.setLayout(new FlowLayout());
		db = new Button("ȷ��");
		lab = new Label("û���ҵ�����ļ�������������");
		d.setBounds(800, 350, 200, 120);
		addWin();
		addAct();
		d.add(lab);
		d.add(db);
		d.setVisible(true);
	}

	private void dialog3() {
		d = new Dialog(main, "����", true);
		d.setLayout(new FlowLayout());
		db = new Button("ȷ��");
		lab = new Label("ȷ��Ҫɾ���𣬴˲��������棡");
		d.setBounds(800, 350, 200, 120);
		addWin();
		db.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				delete();
				d.setVisible(false);
			}
		});
		d.add(lab);
		d.add(db);
		d.setVisible(true);
	}

	private void delete() {
		ta.setText("�ɹ�ɾ���������ļ���\n");
		Iterator<File> it = files.iterator();
		while (it.hasNext()) {
			File file = it.next();
			ta.append(file.getAbsolutePath() + "\n");
			file.delete();
		}
	}

	private boolean find() {
		f = new File(tf1.getText());
		if (!f.exists() || !f.isDirectory()) {
			dialog1();
			return false;
		}
		files = new ArrayList<>();
		addFile(f);
		return true;
	}

	private void addAct() {
		db.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				d.setVisible(false);
			}
		});
	}

	private void addWin() {
		d.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				d.setVisible(false);
			}
		});
	}

	private void addFile(File f) {
		File[] fileList = f.listFiles();
		for (int i = 0; i < fileList.length; i++) {
			File file = fileList[i];
			if (file.isDirectory())
				addFile(file);
			else
				files.add(file);
		}
	}

	private void addEvent() {
		main.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (find() && !tf2.getText().isEmpty()) {
					Iterator<File> it = files.iterator();
					while (it.hasNext()) {
						File file = it.next();
						if (!file.getName().startsWith(tf2.getText()))
							it.remove();
					}
					if (files.isEmpty()) {
						dialog2();
						return;
					} else
						dialog3();
				}
			}
		});
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (find() && !tf2.getText().isEmpty()) {
					Iterator<File> it = files.iterator();
					while (it.hasNext()) {
						File file = it.next();
						if (file.getName().indexOf(tf2.getText()) == -1)
							it.remove();
					}
					if (files.isEmpty()) {
						dialog2();
						return;
					} else
						dialog3();
				}
			}
		});
		b3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (find() && !tf2.getText().isEmpty()) {
					Iterator<File> it = files.iterator();
					while (it.hasNext()) {
						File file = it.next();
						if (!file.getName().endsWith(tf2.getText()))
							it.remove();
					}
					if (files.isEmpty()) {
						dialog2();
						return;
					} else
						dialog3();
				}
			}
		});
	}

	public static void main(String[] args) {
		new DeleteFiles();
	}

}
